#!/bin/bash
###############################
# 裸机服务器安装elk 日志收集工具
#
# elasticsearch-7.7.1
# logstash-7.7.1
# kibana-7.7.1
#
# 1.创建elk用户 默认密码为空
# 2.下载elk安装脚本
#
# author:angy.zhang
# mail: zdy410291479@gmail.com
# link： www.hiext.com
# date:2018/6/10
##############################
create_user(){
    if [ `whoami` = "root" ];then
        echo "root！"
        check_user elk
        if [ $? -eq 0 ];then
            useradd elk
            if [ $? -eq 0 ];then
                echo "user elk is created successfully!!!"
            else
                echo "user elk is created failly!!!"
                return 1
            fi
        else
            echo "user elk is created failly!!!"
            return 1
        fi
    else
        echo "请用root用户执行！"
    fi
}

check_user()
{
	#判断用户是否存在passwd中
	i=`cat /etc/passwd | cut -f1 -d':' | grep -w "$1" -c`
	if [ $i -le 0 ]; then
		echo "User $1 is not in the passwd"
		return 0
	else
	#显示用户存在
		echo "User $1 is in then passwd"
		return 1
	fi
}
load_elk_init(){
    cd /home/elk/
    if [ ! -d "elk_init.sh" ];then
        echo "down elk_init.sh"
    else
        rm -f elk_init.sh
    fi
    wget https://youpin-file.oss-cn-shenzhen.aliyuncs.com/software/shell/elk_init.sh
    chown elk elk_init.sh
    chgrp elk elk_init.sh
    chmod +x elk_init.sh
    su - elk -s /bin/bash /home/elk/elk_init.sh
    echo "success"
}
create_user
load_elk_init